package cytargetlinker.conversion.gml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GmlParser {
	public static void main(String argv[]) throws Exception {
		File input = new File(argv[0]);
		File output = new File(argv[1]);

		BufferedReader br = new BufferedReader(new FileReader(input));
		String line = br.readLine(); //TK: skip header line

		List<List<String>> rows = new ArrayList<List<String>>();
		while((line = br.readLine()) != null) {
			String[] columns = line.split("\t");
			List<String> columnList = Arrays.asList(columns);
			rows.add(columnList);
		}

		//Mapping from textual identifier (gene/miRNA ID) to numeric identifier (as used in GML)
		Map<String, Integer> identifiers = new HashMap<String, Integer>();

		//Generate a unique numeric identifier for each unique miRNA and gene identifier
		int j = 0; //Counter for unique integer identifier
		for (List<String> row : rows) {
			String mirnaID = row.get(1);
			if(!identifiers.containsKey(mirnaID)) {
				identifiers.put(mirnaID, j++); //Increment id counter and link to textual id
			}
			String geneID =  row.get(3);
			if(!identifiers.containsKey(geneID)) {
				identifiers.put(geneID, j++); //Increment id counter and link to textual id
			}
		}

		//Print the resulting HashMap
		//		for(Entry<String, Integer> e : identifiers.entrySet()) {
		//			System.out.println("key: " + e.getKey() + "\t\tvalue: " + e.getValue());
		//		}

		FileOutputStream out = new FileOutputStream(output);
		PrintWriter file = new PrintWriter(out);

		//TK: Always use generics for collections
		//Set set = new HashSet();
		Set<String> set = new HashSet<String>();

		file.println("graph [ ");

		//TK: use iterator variant of for loop (faster than calling .get(i) each time)
		for (List<String> row : rows) {
			if (!set.contains(row.get(3))) {
				set.add(row.get(3));
				file.println("\tnode [ ");
				file.println("\t\tid " + identifiers.get(row.get(3)));
				file.println("\t\tGeneNames " + "\"" + row.get(3) + "\"");
				file.println("\t\tmiRNAEntrezIds " + "\"" + row.get(4) + "\"");
				file.println("\t\tGeneSpecies " + "\"" + row.get(5) + "\"");
				file.println("\t\tlabel " + "\"" + row.get(3) + "\""); //TK: add label for both miRNA and gene
				file.println("\t]");
			}
		}

		for (List<String> row : rows) {
			if (!set.contains(row.get(1))) {
				set.add(row.get(1));
				file.println("\tnode [ ");
				file.println("\t\tid " + identifiers.get(row.get(1)));
				file.println("\t\tmiRTarbaseID " + "\"" + row.get(0) + "\"");
				file.println("\t\tmiRNAEntrezIds " + "\"" + row.get(1) + "\"");
				file.println("\t\tmiRNAspecies " + "\"" + row.get(2) + "\"");
				file.println("\t\tlabel " + "\"" + row.get(1) + "\"");
				file.println("\t]");
			}
		}

		for (List<String> row : rows) {
			file.println("\tedge [ ");
			file.println("\t\tsource " + identifiers.get(row.get(3)));
			file.println("\t\ttarget " + identifiers.get(row.get(1)));
			file.println("\t\texperiments " + "\"" + row.get(6) + "\"");
			file.println("\t\tsupportType " + "\"" + row.get(7) + "\"");
			file.println("\t\tReferenceID " + "\"" + row.get(8) + "\"");
			file.println("\t]");
		}

		file.println("]");
		file.close();

		br.close();
	}
}
